package com.xindaoapp.happypet.activity;

import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.bean.GoodsOne;
import com.xindaoapp.happypet.bean.GoodsOne.Cat;

public class GoodsClassActivity extends UMBaseActivity implements OnClickListener {
    @ViewInject(R.id.goods_one_list)
    private ListView one_list;
    @ViewInject(R.id.shop_second)
    private GridView second_list;
    @ViewInject(R.id.rl_search)
    private RelativeLayout rl_search;

    @ViewInject(R.id.noresult)
    private RelativeLayout noresult;


    @ViewInject(R.id.tittle_shop_cancel)
    private ImageView tittle_shop_cancel;
    @ViewInject(R.id.tittle_shop_content)
    private TextView tittle_shop_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodsclass);
        ViewUtils.inject(this);
        initClick();
        initOneList();

    }

    private void initClick() {
        tittle_shop_content.setText("类目");
        rl_search.setOnClickListener(this);
        tittle_shop_cancel.setOnClickListener(this);
    }

    private List<Cat> cats2;
    private GoodSecondAdapter secondAdapter;

    private void initSecond(String id) {
        RequestParams params = new RequestParams();
        params.put("parentID", id);
        AddClient.get(Address.SHOPGOODSONE, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                second_list.setVisibility(View.VISIBLE);
                noresult.setVisibility(View.GONE);
                try {
                    GoodsOne parse = JSON.parseObject(content, GoodsOne.class);
                    if (parse.getStatus().equals("1")) {
                        cats2 = parse.getData();
                        if (cats2 != null && cats.size() != 0) {
                            secondAdapter = new GoodSecondAdapter(cats2);
                            second_list.setAdapter(secondAdapter);
                            secondAdapter.notifyDataSetChanged();
                        } else {
                            second_list.setVisibility(View.GONE);
                            noresult.setVisibility(View.VISIBLE);
                        }
                    }
                    if (parse.getStatus().equals("0") && cats2.size() == 0) {
                        second_list.setVisibility(View.VISIBLE);
                        Toast.makeText(getApplicationContext(), parse.getMsg(), Toast.LENGTH_SHORT)
                                .show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                Toast.makeText(getApplicationContext(), "网络未连接，请检查您的网络设置", Toast.LENGTH_SHORT)
                        .show();
                super.onFailure(error, content);
            }
        });
        second_list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GoodsClassActivity.this, CatListItem.class);
                intent.putExtra("catID", cats2.get(position).getCat_id());
                intent.putExtra("catName", cats2.get(position).getCat_name());
                startActivity(intent);
            }
        });
    }

    /**
     * 右边种类列表
     */
    private class GoodSecondAdapter extends BaseAdapter {
        private final List<Cat> cats2;

        public GoodSecondAdapter(List<Cat> cats2) {
            super();
            this.cats2 = cats2;
        }

        @Override
        public int getCount() {
            return cats2.size();
        }

        @Override
        public Object getItem(int position) {
            return cats2.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolderSecond holder;
            if (convertView == null) {
                convertView =
                        View.inflate(GoodsClassActivity.this, R.layout.goods_second_item, null);
                holder = new ViewHolderSecond();
                holder.pic = (ImageView) convertView.findViewById(R.id.goods_second_pic);
                holder.name = (TextView) convertView.findViewById(R.id.goods_second_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolderSecond) convertView.getTag();
            }
            holder.name.setText(cats2.get(position).getCat_name());
            if (cats2.get(position).getImg_url() != null
                    && !cats2.get(position).getImg_url().equals("")) {
                ImageLoader.getInstance()
                        .displayImage(cats2.get(position).getImg_url(), holder.pic);
            } else {
                holder.pic.setImageResource(R.drawable.leimu_right_item_bg);
            }

            return convertView;
        }
    }

    class ViewHolderSecond {
        ImageView pic;
        TextView name;
    }

    /**
     * 左边种类列表
     */
    private GoodsOneAdapter oneAdapter;
    private static List<Cat> cats;

    private void initOneList() {
        AddClient.get(Address.SHOPGOODSONE, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                try {
                    GoodsOne parse = JSON.parseObject(content, GoodsOne.class);
                    if (parse.getStatus().equals("1")) {
                        cats = parse.getData();
                        cats.get(0).setChecked(true);
                        initSecond(cats.get(0).getCat_id());
                        if (oneAdapter == null) {
                            oneAdapter = new GoodsOneAdapter(cats);
                            one_list.setAdapter(oneAdapter);
                        } else {
                            oneAdapter.notifyDataSetChanged();
                        }
                    }
                } catch (Exception e) {
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
            }
        });
        one_list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                oneAdapter.setSelectedPosition(position);
                oneAdapter.notifyDataSetChanged();
                initSecond(cats.get(position).getCat_id());
            }
        });
    }

    private class GoodsOneAdapter extends BaseAdapter {
        int selectedPosition = 0;
        private final List<Cat> cats;

        public GoodsOneAdapter(List<Cat> goods) {
            super();
            this.cats = goods;
        }

        @Override
        public int getCount() {
            return cats.size();
        }

        @Override
        public Object getItem(int position) {
            return cats.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void setSelectedPosition(int position) {
            selectedPosition = position;
        }

        @SuppressWarnings("static-access")
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolderGood holder;
            if (convertView == null) {
                convertView = View.inflate(GoodsClassActivity.this, R.layout.goods_one_item, null);
                holder = new ViewHolderGood();
                holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);
                holder.check = (CheckedTextView) convertView.findViewById(R.id.goods_one_check);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolderGood) convertView.getTag();
            }
            if (selectedPosition == position) {
                holder.check.setTextColor(new Color().RED);
                holder.check.setTextSize(18);
                holder.imageView1.setVisibility(View.GONE);
                convertView.setBackgroundResource(R.drawable.leimu_left_3);
            } else {
                holder.check.setTextColor(new Color().GRAY);
                holder.check.setTextSize(16);
                holder.imageView1.setVisibility(View.GONE);
                convertView.setBackgroundResource(R.drawable.leinu_left_bg);
            }
            holder.check.setText(cats.get(position).getCat_name());
            return convertView;
        }
    }

    class ViewHolderGood {
        CheckedTextView check;
        ImageView imageView1;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_search:
                Intent intent = new Intent(this, GoodsSerachActivity.class);
                startActivity(intent);
                break;
            case R.id.tittle_shop_cancel:
                finish();
                break;

            default:
                break;
        }
    }
}
